package com.bodu.system.service.impl;

import com.bodu.core.entity.ParamReq;
import com.bodu.core.entity.enums.DelFlagEnum;
import com.bodu.core.entity.enums.StatusFlagEnum;
import com.bodu.system.entity.request.DicItemRequest;
import com.bodu.system.entity.vo.DicItemResVo;
import com.bodu.system.entity.vo.DicItemVo;
import com.bodu.system.mapper.DicItemMapper;
import com.bodu.system.service.DicItemService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 字典项 Service impl
 *
 * @author Administrator
 */
@Service
public class DicItemServiceImpl implements DicItemService {

    @Resource
    private DicItemMapper dicItemMapper;

    @Override
    public DicItemVo getModel(Long id) {
        return dicItemMapper.getModel(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long save(DicItemRequest request) {
        return dicItemMapper.save(request);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long update(DicItemRequest request) {
        dicItemMapper.update(request);
        return request.getId();
    }

    @Override
    public List<DicItemVo> listPage(ParamReq<DicItemRequest> params) {
        return dicItemMapper.listPage(params);
    }

    @Override
    public Integer countPage(ParamReq<DicItemRequest> params) {
        return dicItemMapper.countPage(params);
    }

    @Override
    public Integer updateDelFlagById(Long id, Integer delFlag) {
        return dicItemMapper.updateDelFlagById(id, delFlag);
    }

    @Override
    public List<DicItemResVo> listDicItem(String dicCode) {
        ParamReq<DicItemRequest> paramReq = new ParamReq<>();
        DicItemRequest dicItemRequest = new DicItemRequest();
        dicItemRequest.setDicCode(dicCode);
        dicItemRequest.setStatusFlag(StatusFlagEnum.USE.getValue());
        dicItemRequest.setDelFlag(DelFlagEnum.NO.getValue());
        paramReq.setEntity(dicItemRequest);
        Integer total = countPage(paramReq);
        paramReq.setCurrent(1);
        paramReq.setSize(total);
        List<DicItemVo> list = listPage(paramReq);
        List<DicItemResVo> res = new ArrayList<>();
        for (DicItemVo dicItemVo : list) {
            DicItemResVo dicItemResVo = new DicItemResVo();
            dicItemResVo.setId(dicItemVo.getId());
            dicItemResVo.setItemName(dicItemVo.getItemName());
            dicItemResVo.setItemValue(dicItemVo.getItemVal());
            dicItemResVo.setDicCode(dicCode);
            res.add(dicItemResVo);
        }
        return res;
    }
}
